// 原始构造函数
/**
 * @class
 * @param {string} name
 * @param {number} age
 * @param {string} country
 */
function Person(name, age, country) {
  this.name = name;
  this.age = age;
  this.country = country || "Unknown";
  console.log("Person constructor called with:", name, age, country);
}

// 在原型上添加方法
Person.prototype.introduce = function () {
  return `Hello, I'm ${this.name}, ${this.age} years old from ${this.country}`;
};

// 使用 bind 创建绑定函数 - 预设 country 参数
const PersonWithDefaultCountry = Person.bind(null, "Alice", 25);
// 注意：第一个参数 (thisArg) 在 new 调用时会被忽略

console.log("=== 使用 new 调用绑定函数 ===");
// 根据 10.4.1.2，此时会调用原始函数的 [[Construct]]
// @ts-expect-error
const person1 = new PersonWithDefaultCountry("USA");
console.log(person1.introduce()); // Hello, I'm Alice, 25 years old from USA
console.log(person1 instanceof Person); // true
console.log(person1 instanceof Object); // true

console.log("\n=== 再次调用，传入不同国家 ===");
// @ts-expect-error
const person2 = new PersonWithDefaultCountry("UK");
console.log(person2.introduce()); // Hello, I'm Alice, 25 years old from UK

console.log("\n=== 验证原型链 ===");
console.log(Object.getPrototypeOf(person1) === Person.prototype); // true
console.log(person1.constructor === Person); // true

// 更复杂的例子：部分参数绑定
/**
 * @param {string} brand
 * @param {string} model
 * @param {number} year
 * @param {string} color
 */
function Car(brand, model, year, color) {
  this.brand = brand;
  this.model = model;
  this.year = year;
  this.color = color;
  console.log(`Car created: ${brand} ${model} ${year} ${color}`);
}

Car.prototype.getInfo = function () {
  return `${this.color} ${this.year} ${this.brand} ${this.model}`;
};

// 绑定部分参数
const CreateToyota = Car.bind(undefined, "Toyota");
const CreateToyotaCamry = CreateToyota.bind(undefined, "Camry");

console.log("\n=== 多层绑定后的 new 调用 ===");
// @ts-expect-error
const car1 = new CreateToyotaCamry(2023, "Red");
// 相当于: new Car('Toyota', 'Camry', 2023, 'Red')
console.log(car1.getInfo()); // Red 2023 Toyota Camry
console.log(car1 instanceof Car); // true
